<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/******************************************************
* Profile			: user 
* Author			: 乔伟
* Create Time		: 2011-10-15 
* Modify Time		: 2011-10-15 
* Modify Profile	:  
******************************************************/
class user extends Admin_Controller { 

	private $id = null;
	
	/******************************************************
	* Profile			: 列表
	* Author			: 乔伟
	* Create Time		: 2011-10-15 
	* Modify Time		: 2011-10-15
	* Modify Profile	:  
	******************************************************/ 
	public function index($page = 1)
	{
		$where = $this->Request();
		
		!isset($where['page_size']) && $where['page_size'] = 20;
		$where['order_by'] = "id desc";
		$users = $this->user_manager->get_list($where);
		
		$page_size = 20; $page = isset($_GET['page'])?$_GET['page']:1;
		// p_r($users);exit;
		$total_count = $this->user_manager->query_result->record_count;
		$page_count = intval($total_count/$page_size) + 1;
		$pre_page = ($page - 1) < 1 ? 1 : ($page - 1);
		$next_page = ($page + 1) > $page_count ? $page_count : ($page + 1);
		
		$get_str = "";
		if(!isset($_GET))
		{
			$get_str = "?";
		}
		else
		{
			$tmp = array();
			foreach($_GET as $key=>$value)
			{
				if($key != "page")
				{
					$tmp[] = "$key=$value";
				}
			}
			$get_str = "?".implode('&',$tmp)."&";
		}
		
		$sp_config = array(
				'base_url' => _U('user/index'.$get_str,true),
				'current_page'=>$page,
				'pre_page'=>$pre_page,
				'next_page'=>$next_page,
				'page_count'=>$page_count
		);
		
		
		$params = array(
				'users' => $users ,
				'sp_config' => $sp_config
		);
		
		$this->load->view("admin/user/v_index",$params);
		
	}

	function check_username($user_name)
	{
		$user = null;
		
		if(isset($_GET['id']))
		{
			$user = $this->user_manager->get(
				array(
					'name'=>$user_name,
					'id !='=>$this->input->get('id')
				),
				User_Get_Type::Multi
			);
		} 
		else 
		{
			$user = $this->user_manager->get($user_name,User_Get_Type::Name);
		}
		
		if($user!=null)
		{
			$this->form_validation->set_message('check_username', '用户名不能重复');
   			return false;
		}
		return true;
	} 
	
	function check_pic($pic)
	{
		$pic = $_FILES['pic'];  
		//var_dump($pic);
		if($pic['name']=='') return true;
		if( $pic["type"] == "image/gif" || 
			$pic["type"] == "image/jpeg"|| 
			$pic["type"] == "image/png" ||
			$pic["type"] == "image/png" ||
			$pic["type"] == "image/bmp"
		)
		{
			if ($pic["error"] > 0)
			{
				$this->form_validation->set_message('check_pic',$pic["error"]); 
				return false;
			}
		}
		else
		{
			$this->form_validation->set_message('check_pic', '只能上传图片文件');
			return false;
		}
		return true;
	}
	
	/******************************************************
	* Profile		: 批准 
	* Author		: 乔伟
	* Create Time		: 2011-10-15
	* Modify Time		: 2011-10-15
	* Modify Profile	:
	******************************************************/
	public function agree()
	{
		if($this->current_user()->role_id ==1) // 只有管理员有权
		{
			$this->user_manager->update(array('is_agreed'=>1),array('id'=>$this->input->get('id')));
		}
		else
		{
			die("You have no privilege to execute this operation");
		}
		$this->goto_last_index_page(); 
	}
	
	/******************************************************
	* Profile		: 编辑
	* Author		: 乔伟
	* Create Time		: 2011-10-15 
	* Modify Time		: 2011-10-15
	* Modify Profile	:  
	******************************************************/
	public function edit()
	{
		//$id = ($id == null)?$this->input->post("id"):$id;
		$id = isset($_GET['id'])?$this->input->get("id"):null;
		if($id == null) // 添加
		{
			$user = $this->build_class('user');
			if(isset($_GET['group_id']))
			{
				$user->usergroup_id = $this->input->get('group_id');
			}
		}
		else
		{
			$user = $this->user_manager->get($id);
		}
		
		if($this->is_postback())
		{
			$this->load->library('form_validation');
		
			/*if($id==null)
			{
				$this->form_validation->set_rules('password', '!', 'trim|required');
			}
			else
			{
				$this->form_validation->set_rules('password', '!', 'trim');
			}*/
			if ($this->form_validation->run('user') == TRUE)
			{
				$user = $this->filter_post($_POST);
				
				if(!empty($_POST['password']))  
				{
					$user["password"] = md5($this->input->post('password'));  
				}
				if($id == null)  // 添加
				{
					$user['create_time'] = Date('Y-m-d');
					$this->user_manager->insert($user);
				}
				else
				{
					$this->user_manager->update($user,array('id'=>$id));
					// redirect(SELF."/user/index");
				}
				if(!empty($_GET['returnurl']))
				{
					$this->goto_url(urldecode($this->input->get('returnurl'))) ;
					exit;
				}
				$this->goto_last_index_page();
			}
		}
		
		$this->load->view("admin/user/v_form",$user);
	
	} 
	
	/******************************************************
	* Profile			: 删除
	* Author			: 乔伟
	* Create Time		: 2011-10-15 
	* Modify Time		: 2011-10-15
	* Modify Profile	:  
	******************************************************/
	public function del($id = null) 
	{
		$id = isset($_GET['id'])?$_GET['id']:$id;
		if(!empty($id) && $id!=1)
		{
			$this->user_manager->delete($id) ;
		}
		// print_r($_POST);exit;
		if(isset($_POST['ids']))
		{
			//echo implode(',', $this->input->post('ids')); exit ;
			foreach($this->input->post('ids') as $id)
			{
				if( $id!=1){
					$this->user_manager->delete($id) ;
				}
			}
		}
		$this->goto_last_index_page();
	}
	
	/******************************************************
	* Profile			: 登录
	* Author			: 乔伟
	* Create Time		: 2011-10-15 
	* Modify Time		: 2011-10-15 
	* Modify Profile	:  
	******************************************************/
	public function login()
	{
		if(!$this->is_postback())
		{
			$params = array
			(
				'error' => null  
			);
		}
		else
		{
			// echo "abc";
			session_start(); 
			$validate_code = $_SESSION['jr_validationcode'] ;
			$name = trim($this->input->post('name'));
			$password = trim($this->input->post('password'));
			$security_code = trim($this->input->post('security_code'));
			
			$user = $this->user_manager->get($name,User_Get_Type::Name);
			//print_r($user);
			$error = null;
			$user == null && $error = "没有此用户";
			$user != null && md5($password)!= $user->password  && $error = "密码错误";
			strtolower($security_code) != strtolower($validate_code) && $error = "验证码错误";
			
			// echo md5($password) . "---" . $user->password;
			
			if($error == null)
			{
				$this->current_user($user); 
				$this->goto_url(_U("entry/index"));
			} 
			
			$params = array
			(
				'error' => $error 
			);
		}
		
		$this->load->view(
			'admin/user/v_login',$params
		);
	}
	/******************************************************
	* Profile			: 编辑个人信息
	* Author			: 宁秀娟
	* Create Time		: 2012-06-14
	* Modify Time		: 2012-06-14
	* Modify Profile	:
	******************************************************/
	public function selfinfo()
	{
		$id = $this->current_user()->id ;
		// echo $id;exit ; 
		$user = $this->user_manager->get($id); 

		if($this->is_postback())
		{  
			// print_r($_POST);exit ; 
			$this->load->library('form_validation'); 
			if ($this->form_validation->run('selfinfo') == TRUE)
			{ 
				$post_user = $this->filter_post();   
				$post_user['name'] = $user->name;
				if(!empty($_POST['password']))  
				{
					$post_user["password"] = md5($this->input->post('password'));  
				}
				
				$this->user_manager->update($post_user,array('id'=>$id));   
				
				$post_user['success'] = true;
				$user = $post_user ; 
				$s_user = $this->current_user(); 
				$s_user->nickname = $user['nickname'];
				$this->current_user($s_user); 
				/*
				if(!empty($_GET['returnurl']))
				{
					$this->goto_url(urldecode($this->input->get('returnurl'))) ;
					exit;
				}
				*/ 
				
			} 
		}
		
		$this->load->view("admin/user/v_selfinfo",$user);
	}
	
	public function logout()
	{
		$this->session->sess_destroy();
		redirect(SELF); 
	}
}
